<?php 
if(KT!='KhiThe') exit;
class news_model extends khithe_mysql{

	public function setCheckednews($ID,$Val)
	{
		$ID = parent::checkStrMySQL($ID);
		$update = "UPDATE ".news." SET Status='".$Val."'  WHERE ID = '".$ID."'";
		$update_qr = parent::UpdateInsertMySQL($update);
		parent::CloseConnection();
		return $update_qr;
	}
	
	public function setCheckedNewOk($ID,$Val)
	{
		$ID = parent::checkStrMySQL($ID);
		$update = "UPDATE ".news." SET NewOk='".$Val."'  WHERE ID = '".$ID."'";
		$update_qr = parent::UpdateInsertMySQL($update);
		parent::CloseConnection();
		return $update_qr;
	}
	
	public function delCheckednews($ID)
	{
		$ID = parent::checkStrMySQL($ID);
		$del = "DELETE FROM ".news." WHERE ID='".$ID."' ";
		$del_qr = parent::DeleteMySQL($del);
		parent::CloseConnection();
		return $del_qr;
	}

	
	public function mdcheckSeoKey($SeoKey)
	{
		$SeoKey = parent::checkStrMySQL($SeoKey);
		$select = "SELECT * FROM ".news." WHERE SeoKey = '".$SeoKey."'";
		$resultSql= parent::SelectMySQL($select);
		$total = mysql_num_rows($resultSql);
		parent::CloseConnection();
		return $total;
	}
		
	public function updateInsertnew($_POST)
	{
		$ID = parent::checkStrMySQL($_POST['ID']);
		$Title = parent::checkStrMySQL($_POST['txt_titlenew']);
		$CategoryID = parent::checkStrMySQL($_POST['cb_select']);
		$Picture = parent::checkStrMySQL($_POST['Picture']);
		$FullContents = parent::checkStrMySQL($_POST['txt_textarea']);
		
		/*SEO*/
		$SeoKey = parent::checkStrMySQL($_POST['txt_seo_key']);
		$TitlePage = parent::checkStrMySQL($_POST['txt_title_page']);
		$KeyWords = parent::checkStrMySQL($_POST['txt_keywords']);
		$Description = parent::checkStrMySQL($_POST['txt_description']);
		$SeoH1 = parent::checkStrMySQL($_POST['txt_seo_h1']);
		$SeoH2 = parent::checkStrMySQL($_POST['txt_seo_h2']);
		$SeoP = parent::checkStrMySQL($_POST['txt_seo_p']);
		
				
		if($_POST['acnew']=='edit'){
			$query = "UPDATE ".news." SET Title='".$Title."', CategoryID='".trim($CategoryID)."', FullContents= '".$FullContents."',SeoKey='".$SeoKey."',TitlePage = '".trim($TitlePage)."',Description = '".$Description."',KeyWords = '".$KeyWords."',SeoH1 = '".$SeoH1."',SeoH2 = '".$SeoH2."',SeoP = '".$SeoP."' WHERE ID = '".trim($ID)."'";
		
		}else if($_POST['acnew']=='add'){
			$query = "INSERT INTO ".news."( Title, CategoryID, FullContents,SeoKey, TitlePage, Description, KeyWords, SeoH1, SeoH2, SeoP, `Status`, PostDate,`Action`) VALUES ('".$Title."', '".trim($CategoryID)."', '".$FullContents."','".$SeoKey."','".trim($TitlePage)."','".$Description."','".$KeyWords."','".$SeoH1."','".$SeoH2."','".$SeoP."', 'flase', '".time()."','view' )";
		}
		
		$query = parent::UpdateInsertMySQL($query);
		
		
		parent::CloseConnection();
		return $query;
		
	}
	
	public function getMenunews($ParentID)
	{
		$ParentID = parent::checkStrMySQL($ParentID);
		$select = "SELECT * FROM ".categorys." WHERE comp='news' AND ParentID = '".$ParentID."' ORDER BY `Order` ASC";
		parent::CloseConnection();
		return parent::SelectMySQL($select);
	}
	
	public function getTags($ID)
	{
		$select = "SELECT * FROM ".news_tags." WHERE News_Id LIKE '%".$ID."%' ";
		$result = parent::SelectMySQL($select);
		$total = mysql_num_rows($result);
		if($total>0)
		{
			while($row = mysql_fetch_assoc($result))
			{
				$arrNewID = explode(',',$row['News_Id']);
				foreach($arrNewID as $NID){
					if($NID==$ID){
						$TagsID .= $row['Tags'].',';
					}
				}
			}
			$TagsID = substr($TagsID,0,-1);
		}
		
		return $TagsID;
	}
	
	public function getSqlnews($ParentID,$page,$limit)
	{
		$ParentID = parent::checkStrMySQL($ParentID);
		
		$strPID = " CategoryID='".$ParentID."'";
		$resultMenu = $this->getMenunews($ParentID);
		while( $rowMenu = mysql_fetch_assoc($resultMenu) )
		{
			$strPID = $strPID." OR CategoryID='".$rowMenu['ID']."'";		
		}
		
		$str = $ParentID=="all"?"":" WHERE $strPID ";
		if($limit!=''){
			$page=($page==''?1:$page);
			$start = ($page - 1)* $limit; 
			$strlimit = "LIMIT $start, $limit";
		}
		else{
			$strlimit ='';
		}		 
		
		$select = "SELECT * FROM ".news." ".$str." ORDER BY ID DESC ".$strlimit." ";
		parent::CloseConnection();
		return parent::SelectMySQL($select);
	}
	
	public function getMenu($ID)
	{
		$ID = parent::checkStrMySQL($ID);
		$select = "SELECT * FROM ".categorys." WHERE comp='news' AND ID = '".$ID."'";
		$query = parent::SelectMySQL($select);
		parent::CloseConnection();
		return $query;
	}
	
	public function selectnew($ID)
	{
		$ID = parent::checkStrMySQL($ID);
		$select = "SELECT * FROM ".news." WHERE ID = '".$ID."'";
		$query = parent::SelectMySQL($select);
		$row = mysql_fetch_assoc($query);
		parent::CloseConnection();
		
		return $row;
	}
	
	public function GetId()
	{
		$select = "SELECT * FROM ".news." ORDER BY ID DESC";
		$query = parent::SelectMySQL($select);
		$row = mysql_fetch_assoc($query);
		parent::CloseConnection();
		
		return $row['ID'];
	}
	
}
?>